$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
	$("[data-type=dateAll]").each(function (){
		$(this).datetimepicker({
			  timeFormat: "HH:mm:ss",
	          dateFormat: "yy-mm-dd",
			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
		});
	});
 	
 	if (url_param_id)
 	{
 		um_ajax_post(
 				"/activity/select.do",
 				{"activityId":url_param_id},
 				function (data){
 					if (data && data.activitys)
 					{
 	 					$("#main-content").umDataBind("render" ,data.activitys[0] ,"");
 	 					$("#previewPicture").next().show();
 	 					$("#previewPicture").next().attr("src" ,data.activitys[0].previewPicture);
 	 					$("#contentPicture").next().show();
 	 					$("#contentPicture").next().attr("src" ,data.activitys[0].previewPicture);
 	 					if (data.activitys[0].isHomePage == 1)
 	 					{
 	 						$("#homePageDiv").show();
 	 						$("#homePageLabel").html("是");
 	 						$("#homePageImage").next().show();
 	 						$("#homePageImage").next().attr("src" ,data.activitys[0].homePageImage);
 	 					}
 	 					else
 	 					{
 	 						$("#homePageLabel").html("否");
 	 					}
 	 					var statusStr = (data.activitys[0].status == 1?"是":"否");
 	 					$("[data-id=status]").html(statusStr);
 	 					var isTopStr = (data.activitys[0].isTop == 1?"是":"否");
 	 					$("[data-id=isTop]").html(isTopStr);
 	 					var expireStr = get_expire_str(data.activitys[0].expire);
 	 					$("[data-id=expire]").html(expireStr);
 					}

 				},
 				null,false);
 	}
 	
	loadingEnd();
	
	list_list(true);
	
	$("#main-content").show();
}

function event_init()
{
    $("#edit").click(function (){
    	activity_edit();
    });
    
	// 返回按钮
	$("#back").click(function (){
		window.location.href = "/activity/direction/index.do";
	});
	
	// 列表图片
    $("#previewPicture_img").dmUploader({
        url: INDEX_WEBAPP + '/activity/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#previewPicture").val(data.result_map.url);
        	$("#previewPicture").next().attr("src" ,data.result_map.url);
        	$("#previewPicture").next().show();
        }
    });
    
	// 轮播图片
    $("#homePageImage_img").dmUploader({
        url: INDEX_WEBAPP + '/activity/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#homePageImage").val(data.result_map.url);
        	$("#homePageImage").next().attr("src" ,data.result_map.url);
        	$("#homePageImage").next().show();
        }
    });
    
	// 活动大图
    $("#contentPicture_img").dmUploader({
        url: INDEX_WEBAPP + '/activity/image/upload.do',
        dataType: 'json',
        allowedTypes: 'image/*',
        extraData: {data:JsonTools.encode({articleId:getQueryString("id")})},
        onUploadSuccess: function(id, data){
        	index_show("上传成功");
        	$("#contentPicture").val(data.result_map.url);
        	$("#contentPicture").next().attr("src" ,data.result_map.url);
        	$("#contentPicture").next().show();
        }
    });
    
	// 翻页事件
	index_initPageEvent("list_list()");
}

function activity_edit()
{
	if (!$("#main-content").validate())
	{
		return false;
	}
	var obj = $("#main-content").umDataBind("serialize" ,"");
	var url = "/activity/add.do";

	if (url_param_id)
	{
		url = "/activity/update.do";
		obj.articleId = url_param_id;
	}
	
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/activity/direction/index.do?flag=" + SUCCESS;
			},
			null);
}

function list_list(isFirst)
{
	if (isFirst)
	{
		index_pageObj.init();
	}
	
	loadingStart();
	
	var obj = $("#query_panel").umDataBind("serialize" ,"");
	obj.currentPage = index_pageObj.currentPage;
	obj.pageSize = index_pageObj.pageSize;
	obj.activityId = url_param_id;
	um_ajax_post(
			INDEX_WEBAPP + "/activity/user/select.do",
			obj,
			function (data){
				$("#page_record_count").text(data.size);
				_render(data.users);
			},
			null);
	function _render(data)
	{
		var buffer = [];
		if (data && data.length >0)
		{
			for (var i=0;i<data.length;i++)
			{
				buffer.push('<tr>');
				buffer.push('<td>'+data[i].userId+'</td>');
				buffer.push('<td>'+data[i].userName+'</td>');
				buffer.push('<td>'+data[i].tel+'</td>');
				buffer.push('<td>'+data[i].userNames+'</td>');
				buffer.push('<tr>');
			}
			$("#list_tbody").html(buffer.join(""));
		}
		else
		{
			index_pageObj.toEndPage();
		}

		index_pageObj.isEndMethod(data);

	}
	


}

function get_expire_str(checkCode)
{
	if (checkCode == 0)
	{
		return "未开始";
	}
	else if (checkCode == 1)
	{
		return "正在开始";
	}
	else
	{
		return "已过期";
	}
	return checkCode;
}